package com.schedule.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.schedule.dao.TickitPriceDao;
import com.schedule.mapper.TickitPriceMapper;
import com.schedule.model.TickitPrice;


@Controller
@RequestMapping("/price")
public class TickitPriceController {
	@Autowired
	private TickitPriceDao tickitPriceDao;
	
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView getAll(
			HttpSession session
			){
		ModelAndView mav=new ModelAndView("price/priceList");
		List<TickitPriceMapper> tpPrices=tickitPriceDao.findAll();
		System.out.println(tpPrices.size());
		mav.addObject("tpPrices",tpPrices);
		 String adminName=session.getAttribute("adminName").toString(); 
		 mav.addObject("name",adminName);
		return mav;
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public ModelAndView delete(
			@RequestParam int tpId
			){
			tickitPriceDao.deletePrice(tpId+"");
		ModelAndView mav=new ModelAndView("redirect:/price");
		return mav;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.GET)
	public ModelAndView update(
			@RequestParam int tpId
			){
		TickitPrice tickitPrice=tickitPriceDao.findById(tpId+"");
		ModelAndView mav=new ModelAndView("price/updatePrice");
		mav.addObject(tickitPrice);
		return mav;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public ModelAndView update(
			@RequestParam String tpId,
			@RequestParam String starting,
			@RequestParam String ending,
			@RequestParam String price
			){
		TickitPrice tpPrice=new TickitPrice();
		tpPrice.setTpId(Integer.parseInt(tpId));
		tpPrice.setStarting(starting);
		tpPrice.setEnding(ending);
		tpPrice.setPrice(Double.parseDouble(price));
		tickitPriceDao.updatePrice(tpPrice);
		ModelAndView mav=new ModelAndView("redirect:/price");
		return mav;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public ModelAndView add(){
		ModelAndView mav=new ModelAndView("price/addPrice");
		return mav;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ModelAndView add(
			@RequestParam String tpId,
			@RequestParam String starting,
			@RequestParam String ending,
			@RequestParam String price
			){
		TickitPrice tickitPrice=new TickitPrice();
		tickitPrice.setTpId(Integer.parseInt(tpId));
		tickitPrice.setStarting(starting);
		tickitPrice.setEnding(ending);
		tickitPrice.setPrice(Double.parseDouble(price));
		tickitPriceDao.addPrice(tickitPrice);
		ModelAndView mav=new ModelAndView("redirect:/price");
		return mav;
	}
}
